package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasAnalyseStationRankingEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasAnalyseStationRankingService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 油站排行榜
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-15 20:42:03
 */
@Controller
@RequestMapping("/gas/analyse/stationranking")
public class GasAnalyseStationRankingController {
    @Autowired
    private GasAnalyseStationRankingService gasAnalyseStationRankingService;
    @Autowired
    private GasStationinfoService gasStationinfoService;
    @Autowired
    private DeptService deptService;
    @Autowired
	private HttpSessionService httpSessionService;
  
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    // @RequiresPermissions("gasAnalyseStationRanking:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody GasAnalyseStationRankingEntity gasAnalyseStationRanking){
    	
    	String currentDeptId = httpSessionService.getCurrentDeptId();
    	
    	List<String> stationIdList = new ArrayList<>();
		LambdaQueryWrapper<GasStationinfoEntity> stationinfoQueryWrapper = Wrappers.lambdaQuery();
		// TODO
		// stationinfoQueryWrapper.eq(GasStationinfoEntity::getPid, "5c8baa4b-081f-41aa-a549-2235b786c7c7");
		// stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
		// stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");

		
		if(Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
			// 县区局
			stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
			stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			stationinfoQueryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);
		} else if(Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
		// }else{
			// 市局
			stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
			stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			stationinfoQueryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0,5));
		}else{
			// 省局
		}
		
		List<GasStationinfoEntity> stationinfoList = gasStationinfoService.list(stationinfoQueryWrapper);
		
		
		for (GasStationinfoEntity gasStationinfoEntity : stationinfoList) {
			stationIdList.add(gasStationinfoEntity.getId());
			
		}
    	
    	
    	Page page = new Page(gasAnalyseStationRanking.getPage(), gasAnalyseStationRanking.getLimit());
        LambdaQueryWrapper<GasAnalyseStationRankingEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if(TsBqUtils.isNotNull(gasAnalyseStationRanking.getStationName())){
			queryWrapper.like(GasAnalyseStationRankingEntity::getStationName,gasAnalyseStationRanking.getStationName());
		}
		
		if(TsBqUtils.isNotNull(gasAnalyseStationRanking.getYearMonths())){
			queryWrapper.eq(GasAnalyseStationRankingEntity::getYearMonths,gasAnalyseStationRanking.getYearMonths());
		}
		queryWrapper.in(GasAnalyseStationRankingEntity::getStationId,stationIdList);

		queryWrapper.orderByAsc(GasAnalyseStationRankingEntity::getRank);

        IPage<GasAnalyseStationRankingEntity> iPage = gasAnalyseStationRankingService.page(page, queryWrapper);
        
        List<GasAnalyseStationRankingEntity> records = iPage.getRecords();
        for (GasAnalyseStationRankingEntity entity : records) {
        	String taxorgcode = entity.getTaxorgcode();
        	SysDept dept = deptService.getById(taxorgcode);
        	if(TsBqUtils.isNotNull(dept)){
        		entity.setTaxorgName(dept.getName());
        	}else{
        		entity.setTaxorgName(taxorgcode);
        	}
        	
		}
        
        return DataResult.success(iPage);
        
    }
    
    
    
    @ApiOperation(value = "加油站售额统计按年月分组")
	@PostMapping("/groupYearMonth")
	@ResponseBody
	public DataResult groupYearMonth(@RequestBody GasAnalyseStationRankingEntity gasAnalyseStationRankingEntity) {
    	
    	Date date = new Date();
        
    	String stationName = gasAnalyseStationRankingEntity.getStationName();
    	
    	String yearMonths = gasAnalyseStationRankingEntity.getYearMonths();
    	if(TsBqUtils.isNull(yearMonths)){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String currentYear = sdf.format(date);
            yearMonths = currentYear;
    	}
    	
    	GasAnalyseStationRankingEntity stationRanking = gasAnalyseStationRankingService.groupYearMonth(gasAnalyseStationRankingEntity);

		List<Object> list = new ArrayList<>();

		List<String> stationRankingList = new ArrayList<>();
		if (TsBqUtils.isNotNull(stationRanking)) {
			stationRankingList.add(stationRanking.getMonth1());
			stationRankingList.add(stationRanking.getMonth2());
			stationRankingList.add(stationRanking.getMonth3());
			stationRankingList.add(stationRanking.getMonth4());
			stationRankingList.add(stationRanking.getMonth5());
			stationRankingList.add(stationRanking.getMonth6());
			stationRankingList.add(stationRanking.getMonth7());
			stationRankingList.add(stationRanking.getMonth8());
			stationRankingList.add(stationRanking.getMonth9());
			stationRankingList.add(stationRanking.getMonth10());
			stationRankingList.add(stationRanking.getMonth11());
			stationRankingList.add(stationRanking.getMonth12());
			
		}


		List<String> monthList = new ArrayList<>();
		monthList.add("1月");
		monthList.add("2月");
		monthList.add("3月");
		monthList.add("4月");
		monthList.add("5月");
		monthList.add("6月");
		monthList.add("7月");
		monthList.add("8月");
		monthList.add("9月");
		monthList.add("10月");
		monthList.add("11月");
		monthList.add("12月");
		
		list.add(stationRankingList);
		list.add(monthList);
		list.add(yearMonths + stationName + "加油站销售额统计");
		return DataResult.success(list);
	}
}
